using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Log;
using GrapeCity.Forguncy.Plugin;
using System;
using System.ComponentModel;
using System.Numerics;
using System.Threading.Tasks;

namespace VectorCalculator
{
    [Icon("pack://application:,,,/VectorCalculator;component/Resources/Icon.png")]
    [Designer("VectorCalculator.Designer.VectorCalculatorServerCommandDesigner, VectorCalculator")]
    [Category("向量计算")]
    public class Normalization : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        [DisplayName("向量")]
        [Description("由浮点数组成的数组，可通过AI大模型生成，形如[0.112,0.9871,...]")]
        public object Number1Exp { get; set; }

        [ComboProperty]
        [DisplayName("归一化算法")]
        public NormalizationType NormalizationTypeInUse { get; set; } = NormalizationType.L2;

        [ResultToProperty]
        [DisplayName("将计算结果返回到变量")]
        public string ResultTo { get; set; } = "Vector";

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {

            var num1 = await dataContext.EvaluateFormulaAsync(this.Number1Exp);

            var vector1 = Convertor.ToVector(num1);

            if (vector1.Length == 0)
                throw new ArgumentException("向量长度不能为0。");

            float[] result = NormalizationWithSIMD.Normalize(vector1, NormalizationTypeInUse);

            dataContext.Parameters[ResultTo] = result;

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "向量归一化";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}
